--玛法大冒险
--#include "data\config\activityconfig\DailyActivity\MulphaAdventureConfig.lua" once

MYDICECOUNT = 1 	--我的骰子个数
BUYDICECOUNT = 2 	--购买的个数
CURDICENUM = 3 		--当前骰子数字
CURROUND = 4 		--圈数
CURINDEX = 5 		--方向
LASTSTEP = 6 		--剩余步数
CURDIR = 7 			--方向
AWARDIDX = 8 		--奖励位置
ISAWARD = 9 		--奖励状态 0 无奖励 1 可以领取 2挑战
ROUNDAWARDSTATE = 10 	--圈数奖励状态table
SPECIALAWARDSTATE = 11 	--特殊奖励领取table
USEONLINECOUNT = 12 --已加的在线次数


MULPHAADVENTUREROUND = 0 	--副本圈数数据
MULPHAADVENTUREINDEX = 1 	--副本位置数据

--活动开始
function MulphaAdventureStart()
	gRankMgr_Clear(RANK_DEFINE_MULPHAADVEN)


	local var = System.getStaticVar()
	var.MulphaAdventureStart = 1
	if Lang.ScriptTips.MulphaAdventureTips014 and Lang.ScriptTips.MulphaAdventureTips014 ~= "" then
		System.broadcastTipmsg(Lang.ScriptTips.MulphaAdventureTips014,ttScreenCenter + ttChatWindow)
	end
end

--活动结束
function MulphaAdventureEnd()
	local var = System.getStaticVar()
	var.MulphaAdventureStart = nil

	if Lang.ScriptTips.MulphaAdventureTips015 and Lang.ScriptTips.MulphaAdventureTips015 ~= "" then
		System.broadcastTipmsg(Lang.ScriptTips.MulphaAdventureTips015,ttScreenCenter + ttChatWindow)
	end
end

--玛法大冒险跨天
function MulphaAdventureCrossDay()
    gRankMgr_Clear(RANK_DEFINE_MULPHAADVEN)
end

--活动状态
function IsMulphaAdventureStart()
	local var = System.getStaticVar()
	--if var.MulphaAdventureStart == 1 then
		return true
	--end
	--return false
end

--初始化数据
function SendMulphaAdventureData(sysarg)
	if not IsMulphaAdventureStart() then
		return
	end
    local var = Actor.getStaticVar(sysarg)
    if not var.days then var.days = {} end
    local mulphaDailyDiceState = var.days.mulphaDailyDiceState or 0

    local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local myDiceCount = myData[MYDICECOUNT] or 0
    --print("myDiceCount ="..myDiceCount)

	local buyDiceCount = myData[BUYDICECOUNT] or 0
    --print("buyDiceCount ="..buyDiceCount)

	local curDiceNum = myData[CURDICENUM] or 0
    --print("curDiceNum ="..curDiceNum)

	local curRound = myData[CURROUND] or 0
    --print("curRound ="..curRound)

	local curIndex = myData[CURINDEX] or 1
    --print("curIndex ="..curIndex)

	local lastStep = myData[LASTSTEP] or 0
    --print("lastStep ="..lastStep)

	local curDir = myData[CURDIR] or 1
    --print("curDir ="..curDir)

	local awardIdx = myData[AWARDIDX] or 0
    --print("awardIdx ="..awardIdx)

	local isAward = myData[ISAWARD] or 0
    --print("isAward ="..isAward)

	local roundAwardState = myData[ROUNDAWARDSTATE] or {}

	local specialAwardState = myData[SPECIALAWARDSTATE] or {}

    local useOnlineCount = myData[USEONLINECOUNT]  or 0
    --print("useOnlineCount ="..useOnlineCount)

    local onlineCount = MulphaAdventureGetOnlineCount(sysarg)
    --print("onlineCount ="..onlineCount)
    if onlineCount > useOnlineCount then
        myDiceCount = onlineCount + myDiceCount - useOnlineCount
        --print("myDiceCount ="..myDiceCount)
    end
    

	--[[
	local config
	if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		local indx = curIndex
		if indx > #MulphaAdventureConfig.specialRound then
			indx = indx % #MulphaAdventureConfig.specialRound
		end
		if indx == 0 then
			indx = #MulphaAdventureConfig.specialRound
		end
		config = MulphaAdventureConfig.specialRound[indx]
	else
		local indx = curIndex
		local addCircle = 0
		if indx > #MulphaAdventureConfig.specialRound then
			indx = indx % #MulphaAdventureConfig.specialRound
			addCircle = math.floor(indx / MulphaAdventureConfig.specialRound)
		end
		if indx == 0 then
			indx = #MulphaAdventureConfig.specialRound
			addCircle = addCircle - 1
		end
		curRound = curRound + addCircle
		config = MulphaAdventureConfig.normalRound[indx]
	end
	]]
	local config = MulphaAdventureConfig.normalRound[curIndex]
	if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		config = MulphaAdventureConfig.specialRound[curIndex]
	end

	if not config then
		return
	end

	--[[
	local eventType = 0
	local eventString = ""
	if not (isAward and isAward == 0) then
		 eventType, eventString = MulphaAdventureGetDesc(config, isAward, awardIdx)
	end
	]]

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureInitData)
    if not netPack then return end

    DataPack.writeByte(netPack, mulphaDailyDiceState)
    DataPack.writeShort(netPack, myDiceCount)
    DataPack.writeShort(netPack, buyDiceCount)
    DataPack.writeByte(netPack, curDiceNum)

    DataPack.writeByte(netPack, curRound)
    DataPack.writeByte(netPack, curIndex)
    DataPack.writeByte(netPack, lastStep)
    DataPack.writeByte(netPack, curDir)
    DataPack.writeByte(netPack, isAward)
    DataPack.writeByte(netPack, awardIdx)

    --[[
    DataPack.writeByte(netPack, eventType)
    DataPack.writeString(netPack, eventString)
    ]]

    local len = #MulphaAdventureConfig.RoundAwards
    DataPack.writeByte(netPack, len)
    for idx = 1, len do
        --print("idx ="..idx)
    	DataPack.writeByte(netPack, idx)
    	local state = roundAwardState[idx] or 0
        --print("state ="..state)
    	DataPack.writeByte(netPack, state)
    end
    
    local rlen = MulphaAdventureConfig.rankCount
    local rankName = RankList[RANK_DEFINE_MULPHAADVEN].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local rankCount = Ranking.getRankItemCountByLimit(ranking, MulphaAdventureConfig.minRankStep)
		if rankCount > MulphaAdventureConfig.rankCount then
			rankCount = MulphaAdventureConfig.rankCount
		end
		if rankCount > 0 then
            DataPack.writeByte(netPack, rankCount)
			for ii=1,rankCount do
				local rankItem = Ranking.getItemFromIndex(ranking, ii-1)
				if rankItem then
					local actorScore = Ranking.getPoint(rankItem) or 0
					local actorName = Ranking.getSub(rankItem, 0)
					DataPack.writeByte(netPack, ii)
                    --print("ii ="..ii)
					DataPack.writeString(netPack, actorName)
                    --print("actorName ="..actorName)
					DataPack.writeInt(netPack, actorScore)
                    --print("actorScore ="..actorScore)
				end
			end	
		else
			DataPack.writeByte(netPack, 0)
		end
	else
		DataPack.writeByte(netPack, 0)
	end

	local myIdx = 0
	local actorid = Actor.getActorId(sysarg)
	if ranking then
		local item = Ranking.getItemPtrFromId( ranking, actorid)
		if item then
			local myScore = Ranking.getPoint(item) or 0
            --print("myScore ="..myScore)
			if myScore < MulphaAdventureConfig.minRankStep then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

    --print("myIdx ="..myIdx)
	DataPack.writeShort(netPack, myIdx)

	local nCount = 0
    local nPos = DataPack.getDataPackPos(netPack)
    DataPack.writeByte(netPack, nCount)
    local slen = #MulphaAdventureConfig.specialRound
    for idx = 1, slen do
    	local state = specialAwardState[idx] or 0
    	if state == 1 then
            --print("idx ="..idx)
    		DataPack.writeByte(netPack, idx)
            --print("state ="..state)
    		DataPack.writeByte(netPack, state)
    		nCount = nCount + 1
    	end
    end
    DataPack.writeByteByPtr(netPack, nPos, nCount)

    DataPack.flush(netPack)
end

--领取骰子
function MulphaAdventureGetDice(sysarg)
	if not IsMulphaAdventureStart() then
		return
	end

	local var = Actor.getStaticVar(sysarg)
    if not var.days then var.days = {} end
    local mulphaDailyDiceState = var.days.mulphaDailyDiceState or 0

    if mulphaDailyDiceState and mulphaDailyDiceState == 1 then
    	Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips004, ttFlyTip)
    	return
    end

    local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local myDiceCount = myData[MYDICECOUNT] or 0

    local useOnlineCount = myData[USEONLINECOUNT]  or 0
    --print("useOnlineCount ="..useOnlineCount)

    local onlineCount = MulphaAdventureGetOnlineCount(sysarg)
    --print("onlineCount ="..onlineCount)

    if onlineCount > useOnlineCount then
        myDiceCount = onlineCount + myDiceCount - useOnlineCount
        myData[USEONLINECOUNT] = onlineCount
        --print("myDiceCount ="..myDiceCount)
    end

	myDiceCount = myDiceCount + MulphaAdventureConfig.dailyGetDiceCount

	myData[MYDICECOUNT] = myDiceCount

	local newData = BaseFuc_serialize(myData)
	gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)

	var.days.mulphaDailyDiceState = 1

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureGetDice)
    if not netPack then return end
    DataPack.writeByte(netPack, 1)
    --print("myDiceCount ="..myDiceCount)
    DataPack.writeShort(netPack, myDiceCount)
    DataPack.flush(netPack)
end

--使用骰子
function MulphaAdventureUseDice(sysarg, packet)
	if not IsMulphaAdventureStart() then
		return
	end
	local choiceType = DataPack.readByte(packet)
     --print("choiceType ="..choiceType)
	local choiceNum
	if choiceType == 2 then
		choiceNum = DataPack.readByte(packet)
        --print("choiceNum ="..choiceNum)
	end
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)
	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local myDiceCount = myData[MYDICECOUNT] or 0

	local buyDiceCount = myData[BUYDICECOUNT] or 0

    local isAward = myData[ISAWARD] or 0
    --print("isAward ="..isAward)

    local useOnlineCount = myData[USEONLINECOUNT]  or 0
    --print("useOnlineCount ="..useOnlineCount)

    local onlineCount = MulphaAdventureGetOnlineCount(sysarg)
    --print("onlineCount ="..onlineCount)

    if onlineCount > useOnlineCount then
        myDiceCount = onlineCount + myDiceCount - useOnlineCount
        --print("myDiceCount ="..myDiceCount)

        myData[USEONLINECOUNT] = onlineCount
        myData[MYDICECOUNT] = myDiceCount
    end

    if isAward ~= 0 then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips019, ttFlyTip)
        return
    end

	local diceNum
	if choiceType == 1 then
		if myDiceCount <= 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips005, ttFlyTip)
			return
		end
		local idx, item = GetItemIdxRand(MulphaAdventureConfig.randomDiceNum)
		diceNum = item.diceNum
		myDiceCount = myDiceCount - 1
		myData[MYDICECOUNT] = myDiceCount
	elseif choiceType == 2 then
		local needGold = MulphaAdventureConfig.buyDicePointNeed + MulphaAdventureConfig.buyDicePointNeedAdd * buyDiceCount

		if Actor.getMoneyCount(sysarg, mtYuanbao) < needGold then
      		Actor.sendItemLack( sysarg, qatYuanbao, 0, needGold )
      		return
   		end

   		if choiceNum and choiceNum < MulphaAdventureConfig.diceMinNum and choiceNum > MulphaAdventureConfig.diceMaxNum then
   			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips006, ttFlyTip)
   			return
   		end

	   	if not Actor.delMoney( sysarg,mtYuanbao,needGold,LogIds.MulphaAdventureLogId,Lang.LogTips.MulphaAdventure) then
	   		return
	   	end

		buyDiceCount = buyDiceCount + 1
		myData[BUYDICECOUNT] = buyDiceCount
		diceNum = choiceNum
	else
		return
	end
	
    --print("diceNum ="..diceNum)
	myData[CURDICENUM] = diceNum
	myData[LASTSTEP] = diceNum
	myData[CURDIR] = 1
	MulphaAdventureCheckStopPosition(sysarg, myData)
	
	local newData = BaseFuc_serialize(myData)
	--print("newData = "..newData)
	gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)

    Actor.triggerQuestEvent(sysarg, 45, 4, 1)

    local playVar = Actor.getStaticVar(sysarg)
    if not playVar.days then playVar.days = {} end
    if not playVar.days.mulphaAdventureState then
        Actor.triggerAchieveEvent(sysarg, 44, 13)
        playVar.days.mulphaAdventureState = 1
    end

	MulphaAdventureUseDiceBack(sysarg)
end

--使用骰子返回
function MulphaAdventureUseDiceBack(sysarg)
	if not IsMulphaAdventureStart() then
		return
	end

    local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local myDiceCount = myData[MYDICECOUNT] or 0
    --print("myDiceCount ="..myDiceCount)

	local buyDiceCount = myData[BUYDICECOUNT] or 0
    --print("buyDiceCount ="..buyDiceCount)

	local curDiceNum = myData[CURDICENUM] or 0
    --print("curDiceNum ="..curDiceNum)

	local curRound = myData[CURROUND] or 0
    --print("curRound ="..curRound)

	local curIndex = myData[CURINDEX] or 1
    --print("curIndex ="..curIndex)

	local lastStep = myData[LASTSTEP] or 0
    --print("lastStep ="..lastStep)

	local curDir = myData[CURDIR] or 1
    --print("curDir ="..curDir)

	local awardIdx = myData[AWARDIDX] or 0
    --print("awardIdx ="..awardIdx)

	local isAward = myData[ISAWARD] or 0
    --print("isAward ="..isAward)

	local roundAwardState = myData[ROUNDAWARDSTATE] or {}

	local specialAwardState = myData[SPECIALAWARDSTATE] or {}

    local useOnlineCount = myData[USEONLINECOUNT]  or 0
    --print("useOnlineCount ="..useOnlineCount)

    local onlineCount = MulphaAdventureGetOnlineCount(sysarg)
    --print("onlineCount ="..onlineCount)
    if onlineCount > useOnlineCount then
        myDiceCount = onlineCount + myDiceCount - useOnlineCount
        --print("myDiceCount ="..myDiceCount)
    end

	--[[
	local config
	if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		local indx = curIndex
		if indx > #MulphaAdventureConfig.specialRound then
			indx = indx % #MulphaAdventureConfig.specialRound
		end
		if indx == 0 then
			indx = #MulphaAdventureConfig.specialRound
		end
		config = MulphaAdventureConfig.specialRound[indx]
	else
		local indx = curIndex
		local addCircle = 0
		if indx > #MulphaAdventureConfig.specialRound then
			indx = indx % #MulphaAdventureConfig.specialRound
			addCircle = math.floor(indx / MulphaAdventureConfig.specialRound)
		end
		if indx == 0 then
			indx = #MulphaAdventureConfig.specialRound
			addCircle = addCircle - 1
		end
		curRound = curRound + addCircle
		config = MulphaAdventureConfig.normalRound[indx]
	end
	]]
	local config = MulphaAdventureConfig.normalRound[curIndex]
	if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		config = MulphaAdventureConfig.specialRound[curIndex]
	end

	if not config then
		return
	end

	--[[
	local eventType = 0
	local eventString = ""
	if not (isAward and isAward == 0) then
		 eventType, eventString = MulphaAdventureGetDesc(config, isAward, awardIdx)
	end
	]]

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureUseDice)
    if not netPack then return end

    DataPack.writeShort(netPack, myDiceCount)
    DataPack.writeShort(netPack, buyDiceCount)
    DataPack.writeByte(netPack, curDiceNum)

    DataPack.writeByte(netPack, curRound)
    DataPack.writeByte(netPack, curIndex)
    DataPack.writeByte(netPack, lastStep)
    DataPack.writeByte(netPack, curDir)
    DataPack.writeByte(netPack, isAward)
    DataPack.writeByte(netPack, awardIdx)

    --[[
    DataPack.writeByte(netPack, eventType)
    DataPack.writeString(netPack, eventString)
    ]]

    local len = #MulphaAdventureConfig.RoundAwards
    DataPack.writeByte(netPack, len)
    for idx = 1, len do
    	DataPack.writeByte(netPack, idx)
        --print("idx ="..idx)
    	local state = roundAwardState[idx] or 0
        --print("state ="..state)
    	DataPack.writeByte(netPack, state)
    end
    
    local rlen = MulphaAdventureConfig.rankCount
    local rankName = RankList[RANK_DEFINE_MULPHAADVEN].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local rankCount = Ranking.getRankItemCountByLimit(ranking, MulphaAdventureConfig.minRankStep)
		if rankCount > MulphaAdventureConfig.rankCount then
			rankCount = MulphaAdventureConfig.rankCount
		end
		if rankCount > 0 then
            DataPack.writeByte(netPack, rankCount)
			for ii=1,rankCount do
				local rankItem = Ranking.getItemFromIndex(ranking, ii-1)
				if rankItem then
					local actorScore = Ranking.getPoint(rankItem) or 0
					local actorName = Ranking.getSub(rankItem, 0)
					DataPack.writeByte(netPack, ii)
                    --print("ii ="..ii)
					DataPack.writeString(netPack, actorName)
                    --print("actorName ="..actorName)
					DataPack.writeInt(netPack, actorScore)
                    --print("actorScore ="..actorScore)
				end
			end	
		else
			DataPack.writeByte(netPack, 0)
		end
	else
		DataPack.writeByte(netPack, 0)
	end

	local myIdx = 0
	local actorid = Actor.getActorId(sysarg)
	if ranking then
		local item = Ranking.getItemPtrFromId( ranking, actorid)
		if item then
			local myScore = Ranking.getPoint(item) or 0
            --print("myScore ="..myScore)
			if myScore < MulphaAdventureConfig.minRankStep then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

    --print("myIdx ="..myIdx)
	DataPack.writeShort(netPack, myIdx)

	local nCount = 0
    local nPos = DataPack.getDataPackPos(netPack)
    DataPack.writeByte(netPack, nCount)
    local slen = #MulphaAdventureConfig.specialRound
    for idx = 1, slen do
    	local state = specialAwardState[idx] or 0
    	if state == 1 then
            --print("idx ="..idx)
    		DataPack.writeByte(netPack, idx)
            --print("state ="..state)
    		DataPack.writeByte(netPack, state)
    		nCount = nCount + 1
    	end
    end
    DataPack.writeByteByPtr(netPack, nPos, nCount)

    DataPack.flush(netPack)
end

--玩家操作
function MulphaAdventureOperate(sysarg, packet)
	if not IsMulphaAdventureStart() then
		return
	end

	local opType = DataPack.readByte(packet)


	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local sendState, resultIndex = MulphaAdventureOperateSub(sysarg, opType, myData)

	if sendState and sendState then
		local newData = BaseFuc_serialize(myData)
		--print("newData = "..newData)
		gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)

		local myDiceCount = myData[MYDICECOUNT] or 0

		local awardIdx = myData[AWARDIDX] or 0

		local isAward = myData[ISAWARD] or 0

		local specialAwardState = myData[SPECIALAWARDSTATE] or {}

        local useOnlineCount = myData[USEONLINECOUNT]  or 0
        --print("useOnlineCount ="..useOnlineCount)

        local onlineCount = MulphaAdventureGetOnlineCount(sysarg)
        --print("onlineCount ="..onlineCount)

        if onlineCount > useOnlineCount then
            myDiceCount = onlineCount + myDiceCount - useOnlineCount
            --print("myDiceCount ="..myDiceCount)
        end

		local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureOperate)
	    if not netPack then return end

        --print("isAward ="..isAward)
	    DataPack.writeByte(netPack, isAward)
        --print("awardIdx ="..awardIdx)
	    DataPack.writeByte(netPack, awardIdx)
        --print("myDiceCount ="..myDiceCount)
	    DataPack.writeShort(netPack, myDiceCount)
	    if resultIndex then
            --print("myDiceCount ="..resultIndex)
	    	DataPack.writeByte(netPack, resultIndex)
            --print("specialAwardState[resultIndex] or 0 ="..(specialAwardState[resultIndex] or 0))
	    	DataPack.writeByte(netPack, specialAwardState[resultIndex] or 0)
	    else
	    	DataPack.writeByte(netPack, 0)
	    	DataPack.writeByte(netPack, 0)
	    end
	    DataPack.flush(netPack)
	end
end

--领取圈数奖励
function MulphaAdventureGetAward(sysarg, packet)
	if not IsMulphaAdventureStart() then
		return
	end

    local index = DataPack.readByte(packet)
    --print("index = "..index)
    local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)

	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local roundAwardState = myData[ROUNDAWARDSTATE] or {}

	if index > #MulphaAdventureConfig.RoundAwards then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips011, ttFlyTip)
		return
	end
	if roundAwardState[index] and roundAwardState[index] == 2 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips013, ttFlyTip)
		return
	end

	if not roundAwardState[index] or roundAwardState[index] == 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips016, ttFlyTip)
		return
	end

	local config = 	MulphaAdventureConfig.RoundAwards[index]
	if not config then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips011, ttFlyTip)
		return
	end

	if config.awards then
		myData[ROUNDAWARDSTATE][index] = 2

		local newData = BaseFuc_serialize(myData)
		--print("newData = "..newData)
		gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)

		if IsBagGridEnough(sysarg, config.awards) then
	        GiveCommonAward(sysarg, config.awards, LogIds.MulphaAdventureLogId, Lang.LogTips.MulphaAdventure)
	    else
	        local actorId = Actor.getActorId(sysarg)
	        local title = Lang.ScriptTips.MulphaAdventureTips017
	        local content = Lang.ScriptTips.MulphaAdventureTips018
	        Awards.GiveToMailById(actorId, config.awards, title, content, LogIds.MulphaAdventureLogId, Lang.LogTips.MulphaAdventure)
	    end
	end

	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureGetAward)
    if not netPack then return end
    --print("index = "..index)
    DataPack.writeByte(netPack, index)
    DataPack.writeByte(netPack, 2)
    DataPack.flush(netPack)
end


--获取排行榜数据
function MulphaAdventureGetRankData(sysarg)
	if not IsMulphaAdventureStart() then
		return
	end
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureRank)
    if not netPack then return end

    local rlen = MulphaAdventureConfig.rankCount
    local rankName = RankList[RANK_DEFINE_MULPHAADVEN].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local rankCount = Ranking.getRankItemCountByLimit(ranking, MulphaAdventureConfig.minRankStep)
		if rankCount > MulphaAdventureConfig.buttonRankCount then
			rankCount = MulphaAdventureConfig.buttonRankCount
		end
		if rankCount > 0 then
            DataPack.writeByte(netPack, rankCount)
			for ii=1,rankCount do
				local rankItem = Ranking.getItemFromIndex(ranking, ii-1)
				if rankItem then
					local actorScore = Ranking.getPoint(rankItem) or 0
					local actorName = Ranking.getSub(rankItem, 0)
					DataPack.writeByte(netPack, ii)
                     --print("ii = "..ii)
                     --print("actorName = "..actorName)
                     --print("actorScore = "..actorScore)
					DataPack.writeString(netPack, actorName)
					DataPack.writeInt(netPack, actorScore)
				end
			end	
		else
			DataPack.writeByte(netPack, 0)
		end
	else
		DataPack.writeByte(netPack, 0)
	end

	local myIdx = 0
	local myScore = 0
	local actorid = Actor.getActorId(sysarg)
	if ranking then
		local item = Ranking.getItemPtrFromId( ranking, actorid)
		if item then
			myScore = Ranking.getPoint(item) or 0
			if myScore < MulphaAdventureConfig.minRankStep then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

    --print("myIdx = "..myIdx)
	DataPack.writeShort(netPack, myIdx)
    --print("myScore = "..myScore)
	DataPack.writeInt(netPack, myScore)

    DataPack.flush(netPack)
end

function MulphaAdventureNoticeRankData(sysarg)
	if not IsMulphaAdventureStart() then
		return
	end
	local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureNoticeRank)
    if not netPack then return end
    local rlen = MulphaAdventureConfig.rankCount
    local rankName = RankList[RANK_DEFINE_MULPHAADVEN].strName
	local ranking = Ranking.getRanking( rankName )
	if ranking then
		local rankCount = Ranking.getRankItemCountByLimit(ranking, MulphaAdventureConfig.minRankStep)
		if rankCount > MulphaAdventureConfig.rankCount then
			rankCount = MulphaAdventureConfig.rankCount
		end
		if rankCount > 0 then
            DataPack.writeByte(netPack, rankCount)
			for ii=1,rankCount do
				local rankItem = Ranking.getItemFromIndex(ranking, ii-1)
				if rankItem then
					local actorScore = Ranking.getPoint(rankItem) or 0
					local actorName = Ranking.getSub(rankItem, 0)
					DataPack.writeByte(netPack, ii)
                    --print("ii = "..ii)
                    --print("actorName = "..actorName)
					DataPack.writeString(netPack, actorName)
                    --print("actorScore = "..actorScore)
					DataPack.writeInt(netPack, actorScore)
				end
			end	
		else
			DataPack.writeByte(netPack, 0)
		end
	else
		DataPack.writeByte(netPack, 0)
	end

	local myIdx = 0
	local actorid = Actor.getActorId(sysarg)
	if ranking then
		local item = Ranking.getItemPtrFromId( ranking, actorid)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < MulphaAdventureConfig.minRankStep then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

    --print("myIdx = "..myIdx)
	DataPack.writeShort(netPack, myIdx)

    DataPack.flush(netPack)
end

function GetMyRankIdx(sysarg)
    local rankName = RankList[RANK_DEFINE_MULPHAADVEN].strName
    local ranking = Ranking.getRanking( rankName )
	local myIdx = 0
	local actorid = Actor.getActorId(sysarg)
	if ranking then
		local item = Ranking.getItemPtrFromId( ranking, actorid)
		if item then
			local myScore = Ranking.getPoint(item) or 0
			if myScore < MulphaAdventureConfig.minRankStep then			--未上榜
				myIdx = 0
			else
				myIdx = Ranking.getIndexFromPtr(item) + 1
			end
		end	
	end

	if myIdx > MulphaAdventureConfig.rankCount then
		myIdx = 0
	end

    --print("myIdx = "..myIdx)

	return myIdx
end

--[[
function MulphaAdventureGetDesc(config, isAward, awardIdx)
	local eventType, eventString
	eventType = config.type
	if eventType == MulphaAdventureEvent.startPoint then
		eventString = config.desc
	elseif eventType == MulphaAdventureEvent.awards then
		local itemName = MulphaAdventureGetItemName(config.awards[1].type, config.awards[1].id)
		eventString = string.format(config.desc, config.awards[1].count, itemName)
	elseif eventType == MulphaAdventureEvent.rest then
		eventString = config.desc
	elseif eventType == MulphaAdventureEvent.randomEvent then
		local awardConfig = config.randomEvent[awardIdx]
		if not awardConfig then
			awardConfig = config.randomEvent[#config.randomEvent]
		end
		local awardDesc = MulphaAdventureGetAwardDesc(awardConfig)
		eventString = string.format(config.desc, awardDesc)
	elseif eventType == MulphaAdventureEvent.fight then
		local monsterName = MulphaAdventureGetMonsterName(config)
		if isAward == 1 then
			eventType = MulphaAdventureEvent.awards
			local awardConfig = config.randomReward[awardIdx]
			if not awardConfig then
				awardConfig = config.randomReward[#config.randomReward]
			end
			local awardDesc = MulphaAdventureGetAwardDesc(awardConfig)
			eventString = string.format(config.awardDesc, awardDesc)
		else
			eventString = string.format(config.desc, monsterName)
		end
	elseif eventType == MulphaAdventureEvent.canNotPassFight then
		local monsterName = MulphaAdventureGetMonsterName(config)
		if isAward == 1 then
			eventType = MulphaAdventureEvent.awards
			local awardConfig = config.randomReward[awardIdx]
			if not awardConfig then
				awardConfig = config.randomReward[#config.randomReward]
			end
			local awardDesc = MulphaAdventureGetAwardDesc(awardConfig)
			eventString = string.format(config.awardDesc, monsterName, awardDesc)
		else
			eventString = string.format(config.desc, monsterName)
		end
	end
	return eventType, eventString
end

function MulphaAdventureGetItemName(type, id)
	local itemName = Item.getAwardDesc(type, id)
	if not itemName then
		itemName = ""
	end
	return itemName
end

function MulphaAdventureGetAwardDesc(awardConfig)
	local desc = ""
	if awardConfig.subType == MulphaAdventureRandomEvent.goAhead then
		desc = string.format(MulphaAdventureTips001, awardConfig.awardNum)
	elseif awardConfig.subType == MulphaAdventureRandomEvent.fallBack then
		desc = string.format(MulphaAdventureTips002, awardConfig.awardNum)
	elseif awardConfig.subType == MulphaAdventureRandomEvent.diceCount then
		desc = string.format(MulphaAdventureTips003, awardConfig.awardNum)
	end
	return desc
end


function MulphaAdventureGetMonsterName(config)
	local monName
	if config.boss and config.boss[1] and config.boss[1].monsterId then
		monName = System.getMonsterNameById(config.boss[1].monsterId)
	end
	if not monName then
		monName = ""
	end
	return monName
end

]]

--获取停下位置
function MulphaAdventureCheckStopPosition(sysarg, myData)
	local len = myData[LASTSTEP]
	for idx = 1, len do
		local state = MulphaAdventureNextStep(sysarg, myData, idx)
		if state and state == true then
            --Actor.triggerQuestEvent(sysarg, 45, 4, idx)
			break
		end
	end
end

--每一步状态判断
function MulphaAdventureNextStep(sysarg, myData, idx)
	local curRound = myData[CURROUND] or 0

	local curIndex = myData[CURINDEX] or 1

	local lastStep = myData[LASTSTEP] or 0

	local curDir = myData[CURDIR] or 1

	local awardIdx = myData[AWARDIDX] or 0

	local isAward = myData[ISAWARD] or 0

	local roundAwardState = myData[ROUNDAWARDSTATE] or {}
	local specialAwardState = myData[SPECIALAWARDSTATE] or {}
	if curDir == 1 then
		curIndex = curIndex + 1
	elseif curDir == 2 then
		curIndex = curIndex - 1
		if curIndex <= 0 then
			return
		end
	end
	local isSpecialAward = false
	local config
	if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		if curIndex > #MulphaAdventureConfig.specialRound then
            curRound = curRound + 1
			curIndex = curIndex - #MulphaAdventureConfig.specialRound
		end
		config = MulphaAdventureConfig.specialRound[curIndex]
		isSpecialAward = true
	else
		if curIndex > #MulphaAdventureConfig.normalRound then
			curRound = curRound + 1
			curIndex = curIndex - #MulphaAdventureConfig.normalRound
			if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
				if curIndex > #MulphaAdventureConfig.specialRound then
                    local addRound = math.floor((curIndex - 1)/#MulphaAdventureConfig.specialRound)
                    curRound = curRound + addRound
					curIndex = curIndex % #MulphaAdventureConfig.specialRound
				end
				if curIndex == 0 then
					curIndex = #MulphaAdventureConfig.specialRound
				end
				config = MulphaAdventureConfig.specialRound[curIndex]
				isSpecialAward = true
			else
				config = MulphaAdventureConfig.normalRound[curIndex]
			end
		else
			config = MulphaAdventureConfig.normalRound[curIndex]
		end
	end
	if not config then
		return
	end

	if myData[CURROUND] ~= curRound then
		UpdateMulphaAdventureRoundAward(myData, curRound)
	end
	myData[CURROUND] = curRound
	myData[CURINDEX] = curIndex
	myData[LASTSTEP] = myData[LASTSTEP] - 1
	myData[AWARDIDX] = 0
	myData[ISAWARD] = 0

	--local myOldIdx = GetMyRankIdx(sysarg)
	local actorId = Actor.getActorId(sysarg)
	gRankMgr_AddValue(actorId, RANK_DEFINE_MULPHAADVEN, 1)
	local myNewIdx = GetMyRankIdx(sysarg)
	--if myNewIdx ~= myOldIdx then
    if myNewIdx > 0 then
		MulphaAdventureNoticeRankData(sysarg)
	end

	if isSpecialAward and specialAwardState[curIndex] and specialAwardState[curIndex] == 1 then
        --print("isSpecialAward is get curIndex ="..curIndex)
	elseif myData[LASTSTEP] ~= 0 and config.eventType == MulphaAdventureEvent.canNotPassFight then
		myData[ISAWARD] = 2
		return true
	elseif myData[LASTSTEP] == 0 then
		if config.eventType == MulphaAdventureEvent.canNotPassFight or config.eventType == MulphaAdventureEvent.fight then
			myData[ISAWARD] = 2
		elseif config.eventType == MulphaAdventureEvent.randomEvent or config.eventType == MulphaAdventureEvent.awards then
			myData[ISAWARD] = 1
			if config.eventType == MulphaAdventureEvent.randomEvent then
				local ii, item = GetItemIdxRand(config.randomEvent)
				myData[AWARDIDX] = ii
			end
		end
		return true
	end
end

--更新圈数奖励
function UpdateMulphaAdventureRoundAward(myData, curRound)
	if not myData[ROUNDAWARDSTATE] then
		myData[ROUNDAWARDSTATE] = {}
	end
	local roundAwardState = myData[ROUNDAWARDSTATE]
	local len = #MulphaAdventureConfig.RoundAwards
	for idx = 1, len do
		if not roundAwardState[idx] or roundAwardState[idx] == 0 then
			local cfg = MulphaAdventureConfig.RoundAwards[idx]
			if cfg.RoundCount <= curRound then
				roundAwardState[idx] = 1
			end
		end
	end
end

--操作处理
function MulphaAdventureOperateSub(sysarg, opType, myData)

    local actorid = Actor.getActorId(sysarg)

	local myDiceCount = myData[MYDICECOUNT] or 0

	local curDiceNum = myData[CURDICENUM] or 0

	local curRound = myData[CURROUND] or 0

	local curIndex = myData[CURINDEX] or 1

	local lastStep = myData[LASTSTEP] or 0

	local curDir = myData[CURDIR] or 1

	local awardIdx = myData[AWARDIDX] or 0

	local isAward = myData[ISAWARD] or 0

	if not myData[SPECIALAWARDSTATE] then
		myData[SPECIALAWARDSTATE] = {}
	end
	local specialAwardState = myData[SPECIALAWARDSTATE]


	local isSpecialAward = false
	local config = MulphaAdventureConfig.normalRound[curIndex]
	if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		config = MulphaAdventureConfig.specialRound[curIndex]
		isSpecialAward = true
	end

	if not config then
		return
	end

	if config.eventType == MulphaAdventureEvent.randomEvent then
		if opType ~= 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips009, ttFlyTip)
			return
		end
		if isAward ~= 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips010, ttFlyTip)
			return
		end
		if awardIdx == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips011, ttFlyTip)
			return
		end
		if lastStep ~= 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips012, ttFlyTip)
			return
		end
		if isSpecialAward and specialAwardState[curIndex] and specialAwardState[curIndex] == 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips013, ttFlyTip)
			return
		end
		local awardConfig = config.randomEvent[awardIdx]
		if not awardConfig then
			return
		end
		if awardConfig.subType == MulphaAdventureRandomEvent.goAhead then
			--myData[CURDICENUM] = awardConfig.awardNum
			myData[LASTSTEP] = awardConfig.awardNum
			myData[CURDIR] = 1
			myData[ISAWARD] = 0
			myData[AWARDIDX] = 0
			if isSpecialAward then
				specialAwardState[curIndex] = 1
			end
			MulphaAdventureCheckStopPosition(sysarg, myData)

			local newData = BaseFuc_serialize(myData)
			--print("newData = "..newData)
			gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)
			MulphaAdventureUseDiceBack(sysarg)
		elseif awardConfig.subType == MulphaAdventureRandomEvent.fallBack then
			myData[LASTSTEP] = awardConfig.awardNum
			myData[CURDIR] = 2
			myData[ISAWARD] = 0
			myData[AWARDIDX] = 0
			if isSpecialAward then
				specialAwardState[curIndex] = 1
			end
			MulphaAdventureCheckStopPosition(sysarg, myData)

			local newData = BaseFuc_serialize(myData)
			--print("newData = "..newData)
			gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)
			MulphaAdventureUseDiceBack(sysarg)
		elseif awardConfig.subType == MulphaAdventureRandomEvent.diceCount then
			myData[MYDICECOUNT] = awardConfig.awardNum + myDiceCount
			myData[ISAWARD] = 0
			myData[AWARDIDX] = 0
			if isSpecialAward then
				specialAwardState[curIndex] = 1
				return true, curIndex
			end
			return true
		end
	elseif config.eventType == MulphaAdventureEvent.awards then
		if opType ~= 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips009, ttFlyTip)
			return
		end
		if isAward ~= 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips010, ttFlyTip)
			return
		end
        --[[
		if awardIdx == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips011, ttFlyTip)
			return
		end
        ]]
		if lastStep ~= 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips012, ttFlyTip)
			return
		end
		if isSpecialAward and specialAwardState[curIndex] and specialAwardState[curIndex] == 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips013, ttFlyTip)
			return
		end
		if IsBagGridEnough(sysarg, config.awards) then
	        GiveCommonAward(sysarg, config.awards, LogIds.MulphaAdventureLogId, Lang.LogTips.MulphaAdventure)
	    else
            Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
            return
            --[[
	        local actorId = Actor.getActorId(sysarg)
	        local title = Lang.ScriptTips.MulphaAdventureTips007
	        local content = Lang.ScriptTips.MulphaAdventureTips008
	        Awards.GiveToMailById(actorId, config.awards, title, content, LogIds.MulphaAdventureLogId, Lang.LogTips.MulphaAdventure)
            ]]
	    end
	    myData[ISAWARD] = 0
		myData[AWARDIDX] = 0
		if isSpecialAward then
			specialAwardState[curIndex] = 1
			return true, curIndex
		end
		return true
	elseif config.eventType == MulphaAdventureEvent.fight then
		if lastStep ~= 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips012, ttFlyTip)
			return
		end
		if isSpecialAward and specialAwardState[curIndex] and specialAwardState[curIndex] == 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips013, ttFlyTip)
			return
		end
		return MulphaAdventureFight(sysarg, config, opType, myData, isSpecialAward)
	elseif config.eventType == MulphaAdventureEvent.canNotPassFight then
		if isSpecialAward and specialAwardState[curIndex] and specialAwardState[curIndex] == 1 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips013, ttFlyTip)
			return
		end
		return MulphaAdventureFight(sysarg, config, opType, myData, isSpecialAward)
	end
end

--副本挑战
function MulphaAdventureFight(sysarg, config, opType, myData, isSpecialAward)

    local actorid = Actor.getActorId(sysarg)

	local myDiceCount = myData[MYDICECOUNT] or 0

	local curDiceNum = myData[CURDICENUM] or 0

	local curRound = myData[CURROUND] or 0

	local curIndex = myData[CURINDEX] or 1

	local lastStep = myData[LASTSTEP] or 0

	local curDir = myData[CURDIR] or 1

	local awardIdx = myData[AWARDIDX] or 0

	local isAward = myData[ISAWARD] or 0

	if not myData[SPECIALAWARDSTATE] then
		myData[SPECIALAWARDSTATE] = {}
	end
	local specialAwardState = myData[SPECIALAWARDSTATE]

	if isAward == 2 then
        local fubenPtr  = Actor.getFubenPrt(sysarg)
        local fubenId = Fuben.getFuBenId(fubenPtr)            
        if fubenId and fubenId == config.fubenId then
            Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips020, ttFlyTip)
            return
        end
		if opType == 1 then
			if not CheckConsumeCond( sysarg, config.passConsumes, 1 ) then
				return
			end

			if not DoConsumeCond( sysarg, config.passConsumes, 1, LogIds.MulphaAdventureLogId, Lang.LogTips.MulphaAdventure ) then
				return
			end
			myData[ISAWARD] = 0
			myData[AWARDIDX] = 0
			if lastStep > 0 then
				MulphaAdventureCheckStopPosition(sysarg, myData)

				local newData = BaseFuc_serialize(myData)
				--print("newData = "..newData)
				gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)
				MulphaAdventureUseDiceBack(sysarg)
			else
				return true
			end
		elseif opType == 2 then
			MulphaAdventureEnterFight(sysarg, config, curRound, curIndex)
		else
			return
		end
	elseif isAward == 1 and opType == 1 then
		if awardIdx == 0 then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.MulphaAdventureTips011, ttFlyTip)
			return
		end
		local awardConfig = config.randomReward[awardIdx]
		if not awardConfig then
			return
		end
		if awardConfig.subType == MulphaAdventureRandomEvent.goAhead then
			--myData[CURDICENUM] = awardConfig.awardNum
            myData[ISAWARD] = 0
            myData[AWARDIDX] = 0
            if isSpecialAward then
                specialAwardState[curIndex] = 1
            end
			if lastStep > 0 then
				if curDir == 1 then
					myData[LASTSTEP] = awardConfig.awardNum + myData[LASTSTEP]
				elseif myData[LASTSTEP] > awardConfig.awardNum then
					myData[LASTSTEP] = myData[LASTSTEP] - awardConfig.awardNum
				elseif myData[LASTSTEP] < awardConfig.awardNum then
					myData[CURDIR] = 1
					myData[LASTSTEP] = awardConfig.awardNum - myData[LASTSTEP]
				else
					if isSpecialAward then
						return true, curIndex
					end
					return true
				end
			else
				myData[LASTSTEP] = awardConfig.awardNum
				myData[CURDIR] = 1
			end
			MulphaAdventureCheckStopPosition(sysarg, myData)

			local newData = BaseFuc_serialize(myData)
			--print("newData = "..newData)
			gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)
			MulphaAdventureUseDiceBack(sysarg)
		elseif awardConfig.subType == MulphaAdventureRandomEvent.fallBack then
            myData[ISAWARD] = 0
            myData[AWARDIDX] = 0
            if isSpecialAward then
                specialAwardState[curIndex] = 1
            end
			if lastStep > 0 then
				if curDir == 2 then
					myData[LASTSTEP] = awardConfig.awardNum + myData[LASTSTEP]
				elseif myData[LASTSTEP] > awardConfig.awardNum then
					myData[LASTSTEP] = myData[LASTSTEP] - awardConfig.awardNum
				elseif myData[LASTSTEP] < awardConfig.awardNum then
					myData[CURDIR] = 2
					myData[LASTSTEP] = awardConfig.awardNum - myData[LASTSTEP]
				else
					if isSpecialAward then
						return true, curIndex
					end
					return true
				end
			else
				myData[LASTSTEP] = awardConfig.awardNum
				myData[CURDIR] = 2
			end
			MulphaAdventureCheckStopPosition(sysarg, myData)

			local newData = BaseFuc_serialize(myData)
			--print("newData = "..newData)
			gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)
			MulphaAdventureUseDiceBack(sysarg)
		elseif awardConfig.subType == MulphaAdventureRandomEvent.diceCount then
            myData[MYDICECOUNT] = awardConfig.awardNum + myDiceCount
            myData[ISAWARD] = 0
            myData[AWARDIDX] = 0
            if isSpecialAward then
                specialAwardState[curIndex] = 1
            end
            if lastStep > 0 then
                MulphaAdventureCheckStopPosition(sysarg, myData)

                local newData = BaseFuc_serialize(myData)
                --print("newData = "..newData)
                gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)
                MulphaAdventureUseDiceBack(sysarg)
            else
                if isSpecialAward then
                    return true, curIndex
                end
                return true
            end
		end
	else
		return
	end
end

--进入挑战
function MulphaAdventureEnterFight(sysarg, config, curRound, curIndex)
	local MulphaAdventureConfig = config
    if Actor.getIsDartFlag(sysarg) then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.EscortTips010, ttFlyTip)
        return
    end
	if CommonEnterScence( sysarg,  MulphaAdventureConfig, LogIds.MulphaAdventureLogId, Lang.LogTips.MulphaAdventure,
		 MulphaAdventureCheck, MulphaAdventureBeforeEnter ) then

		local actorId 	= Actor.getActorId( sysarg )
		local fubenPtr 	= Actor.getFubenPrt( sysarg )
		Fuben.setOwnerId( fubenPtr, actorId )

		Fuben.setFbValue( fubenPtr, MULPHAADVENTUREROUND, curRound)
		Fuben.setFbValue( fubenPtr, MULPHAADVENTUREINDEX, curIndex)
		--local fubenHandle  	= Fuben.getFubenHandle(fubenPtr)
		--Fuben.setFubenTime(fubenHandle, MulphaAdventureConfig.fubenTime)
		MulphaAdventureFreshMonster( sysarg, MulphaAdventureConfig )
		InitMulphaAdventurePanel( sysarg, MulphaAdventureConfig)
	end
end

--获取掉线重进配置
function GetElemsForReLoginMulphaAdventure(fubenPtr)
	local Round 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREROUND )
	local Index 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREINDEX )
	--print("GetElemsForReLoginGuildBoss, bossId="..bossId)
	local fubenCfg 	= GetMulphaAdventureCfgByRoundAndIndex( Round,  Index)
	local InitFubenRightPanel 	= InitMulphaAdventurePanel
	local loginLog   			= "mulpha adventure login" 
	local loginLogOk			= "mulpha adventure login ok" 
	return fubenCfg, InitFubenRightPanel, loginLog, loginLogOk
end

--获取配置
function GetMulphaAdventureCfgByRoundAndIndex(round, index)
	local config = MulphaAdventureConfig.normalRound[index]
	if round >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
		config = MulphaAdventureConfig.specialRound[index]
		isSpecialAward = true
	end
	return config
end

--[[
副本时间到
]]
function OnMulphaAdventureTimeout( fubenPtr )
	local fubenId = Fuben.getFuBenId(fubenPtr)
	--print("OnMulphaAdventureTimeout, fubenId="..fubenId)
	local Round 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREROUND )
	local Index 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREINDEX )
	--print("GetElemsForReLoginGuildBoss, bossId="..bossId)
	local MulphaAdventureConfig 	= GetMulphaAdventureCfgByRoundAndIndex( Round,  Index)
	if not MulphaAdventureConfig then
		return
	end
	local fubenHandle = Fuben.getFubenHandle(fubenPtr)

	local actorId 	= Fuben.getOwnerId( fubenPtr )
	local actorPtr 	= Actor.getActorById( actorId )

	if actorPtr then
		local actorFubenPtr = Actor.getFubenPrt(actorPtr)
		local actorFubenHandle = Fuben.getFubenHandle(actorFubenPtr)
		if actorFubenHandle ~= fubenHandle then
			return
		end
	end
	--print("OnMulphaAdventureTimeout, actorId="..actorId)
	if actorPtr and IsInMulphaAdventureFuben( actorPtr ) then
		Actor.exitFuben( actorPtr )	
	end
end

--退出
function OnMulphaAdventureExitHandler(sysarg)
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId = Fuben.getFuBenId(fubenPtr)

	local Round 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREROUND )
	local Index 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREINDEX )
	--print("GetElemsForReLoginGuildBoss, bossId="..bossId)
	local MulphaAdventureConfig 	= GetMulphaAdventureCfgByRoundAndIndex( Round,  Index)
	if not MulphaAdventureConfig then
		return
	end

	local actorId 	= Fuben.getOwnerId( fubenPtr )
	local actorPtr 	= Actor.getActorById( actorId )
	local fubenHandle = Fuben.getFubenHandle(fuBenPtr)
	--print("OnMulphaAdventureTimeout, actorId="..actorId)
	if actorPtr and IsInMulphaAdventureFuben( actorPtr ) then
		Fuben.setFubenTime( fubenHandle, 1 )	
	end
end

--[[
副本中下线
]]
function OnLogoutMulphaAdventure( sysarg )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId 	= Fuben.getFuBenId(fubenPtr)

	--print("OnLogoutnMulphaAdventure, sceneId="..sceneId..", fubenId="..fubenId)
	DroplineSaveFubenHandler( sysarg )

	local actorId 	= Actor.getActorId( sysarg )
	local actorName	= Actor.getName( sysarg )
	local sceneId 	= Actor.getSceneId( sysarg )
	local fubenId 	= Actor.getFubenId( sysarg )
	System.sendCommonLog( GameLog.clFubenDropReset_Logout, actorId, actorName , 0, sceneId, fubenId,'','',"fuben logout")
end


--是否副本中
function IsInMulphaAdventureFuben( sysarg )
	local fubenPtr 	= Actor.getFubenPrt(sysarg)
	local fubenId = Fuben.getFuBenId(fubenPtr)
	local Round 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREROUND )
	local Index 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREINDEX )
	--print("GetElemsForReLoginGuildBoss, bossId="..bossId)
	local fubenCfg 	= GetMulphaAdventureCfgByRoundAndIndex( Round,  Index)
	if fubenCfg and fubenCfg.fubenId == fubenId then
		return true
	end
	return false
end

--左侧面板
function InitMulphaAdventurePanel( sysarg, MulphaAdventureCfg )
	local panelInfo = {}
	panelInfo.title = Lang.ScriptTips.MulphaAdventurePanel001									--title
	panelInfo.subActivityId = MulphaAdventureCfg.fubenId

	panelInfo.awardTitle = ""
	panelInfo.awards 	 = {}
	panelInfo.buttons  	 = {1}
	local fubenPtr 		= Actor.getFubenPrt( sysarg )
	local restTime 	= Fuben.getFbTime( fubenPtr )
	--剩余时间
	panelInfo.timeTitle 	= Lang.ScriptTips.MulphaAdventurePanel002
	panelInfo.restTime		= restTime

	panelInfo.contentTitle = Lang.ScriptTips.MulphaAdventurePanel003
	panelInfo.contents = {}

	local handle = Actor.getSceneHandle(sysarg)

	if MulphaAdventureCfg.monsters and #MulphaAdventureCfg.monsters > 0 then
		for k,v in pairs(MulphaAdventureCfg.monsters)do
			local monName = MonsterNameDeleteNumber(v.monsterId)	
			local count = Fuben.getLiveMonsterCount(handle,v.monsterId) or 0	
			count = v.num - count
			table.insert(panelInfo.contents, string.format(Lang.ScriptTips.MulphaAdventurePanel004,monName, count , v.num))
		end
	end

	if MulphaAdventureCfg.boss and #MulphaAdventureCfg.boss > 0 then
		for k,v in pairs(MulphaAdventureCfg.boss)do
			local monName = MonsterNameDeleteNumber(v.monsterId)	
			local count = Fuben.getLiveMonsterCount(handle,v.monsterId) or 0	
			count = v.num - count
			table.insert(panelInfo.contents, string.format(Lang.ScriptTips.MulphaAdventurePanel004,monName, count , v.num))
		end
	end
	local panelType = 1
	local activityId = enRightPanelActivity_MulphaAdventrue

	OpenActivityRightPanel( sysarg, panelType, activityId, panelInfo )
end

--[[
刷怪
]]
function MulphaAdventureFreshMonster( sysarg, bossCfg )
	local fubenPtr 		= Actor.getFubenPrt(sysarg)
	local fubenHandle 	= Fuben.getFubenHandle(fubenPtr)
	local boss = bossCfg.boss
	local monsters = bossCfg.monsters
	if boss then
		for idx = 1, #boss do
			freshSceneMonsterInPosForce(boss[idx], fubenHandle)
		end
	end

	if monsters and #monsters > 0 then
		for idx = 1, #monsters do
			freshSceneMonsterInRangeForce(monsters[idx], fubenHandle)
		end
	end 
end


--[[
杀怪
]]
function OnMulphaAdventureMonsterDie( monster, sysarg, monId )
	
	--print("OnMulphaAdventureMonsterDie, monId="..monId..", cityIdx="..cityCfg.cityIdx)

	local fubenPtr = Actor.getFubenPrt( monster )				--怪物所在副本
	local fubenId= Fuben.getFuBenId( fubenPtr )

	local Round 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREROUND )
	local Index 	= Fuben.getFbValue( fubenPtr, MULPHAADVENTUREINDEX )
	--print("GetElemsForReLoginGuildBoss, bossId="..bossId)
	local MulphaAdventureConfig = GetMulphaAdventureCfgByRoundAndIndex( Round,  Index)
	if not MulphaAdventureConfig then
		return
	end

	if not MulphaAdventureConfig.fubenId then
		return
	end

	if MulphaAdventureConfig.fubenId ~= fubenId then
		return
	end

	local actorId 	= Fuben.getOwnerId( fubenPtr )
	local actorPtr 	= Actor.getActorById( actorId )
	--print("OnMulphaAdventureMonsterDie, actorId="..actorId)
	if actorPtr and IsInMulphaAdventureFuben( actorPtr ) then 			--玩家在线并且在副本中

		local panelInfo 	= {}
		panelInfo.contents 	= {}

		local restTime 	= Fuben.getFbTime( fubenPtr )
		
		--print("ChangeMaterialFubenRightPanel, monNumAll="..monNumAll..", monNumNow="..monNumNow..", restTime="..restTime)

		
		panelInfo = {}
		panelInfo.contents = {}
		
		local handle = Actor.getSceneHandle(sysarg)

		if MulphaAdventureConfig.monsters and #MulphaAdventureConfig.monsters > 0 then
			for k,v in pairs(MulphaAdventureConfig.monsters)do
				local monName = MonsterNameDeleteNumber(v.monsterId)	
				local count = Fuben.getLiveMonsterCount(handle,v.monsterId) or 0	
				count = v.num - count
				table.insert(panelInfo.contents, string.format(Lang.ScriptTips.MulphaAdventurePanel004,monName, count , v.num))
			end
		end

		if MulphaAdventureConfig.boss and #MulphaAdventureConfig.boss > 0 then
			for k,v in pairs(MulphaAdventureConfig.boss)do
				local monName = MonsterNameDeleteNumber(v.monsterId)	
				local count = Fuben.getLiveMonsterCount(handle,v.monsterId) or 0	
				count = v.num - count
				table.insert(panelInfo.contents, string.format(Lang.ScriptTips.MulphaAdventurePanel004,monName, count , v.num))
			end
		end

		ChangeActivityRightPanel( sysarg, panelInfo )
		

		local count = Fuben.getLiveMonsterCount(handle,0) or 0 
		if(count == 0)then
			MulphaAdventurePassFuben(sysarg)
			Actor.exitFuben( actorPtr )	
			Fuben.setFubenTime( fubenHandle, 1 )
		end
	end
end

--副本胜利
function MulphaAdventurePassFuben(sysarg)
	local actorid = Actor.getActorId(sysarg)
	local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
	local myData = BaseFuc_unserialize(data)
	if not myData or myData == "" or myData == 0 then
		myData = {}
	end

	local myDiceCount = myData[MYDICECOUNT] or 0

	local curRound = myData[CURROUND] or 0

	local curIndex = myData[CURINDEX] or 1

	local awardIdx = myData[AWARDIDX] or 0

	local isAward = myData[ISAWARD] or 0

    local useOnlineCount = myData[USEONLINECOUNT]  or 0
    --print("useOnlineCount ="..useOnlineCount)

    local onlineCount = MulphaAdventureGetOnlineCount(sysarg)
    --print("onlineCount ="..onlineCount)

    if onlineCount > useOnlineCount then
        myDiceCount = onlineCount + myDiceCount - useOnlineCount
        --print("myDiceCount ="..myDiceCount)
    end

	local config = GetMulphaAdventureCfgByRoundAndIndex( curRound,  curIndex)
	if not config then
		return
	end


	if config.eventType == MulphaAdventureEvent.fight or config.eventType == MulphaAdventureEvent.canNotPassFight then
		local isSpecialAward = false
		if curRound >= MulphaAdventureConfig.SpecialRoundAppearNeedCount then
			isSpecialAward = true
		end
		local idx, item = GetItemIdxRand(config.randomReward)
		awardIdx = idx
		myData[AWARDIDX] = idx
		myData[ISAWARD] = 1
		isAward = 1
		
		local newData = BaseFuc_serialize(myData)
		--print("newData = "..newData)
		gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)

		local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureOperate)
	    if not netPack then return end

        --print("isAward = "..isAward)
	    DataPack.writeByte(netPack, isAward)

        --print("awardIdx = "..awardIdx)
	    DataPack.writeByte(netPack, awardIdx)
        --print("myDiceCount = "..myDiceCount)
	    DataPack.writeShort(netPack, myDiceCount)
	
    	DataPack.writeByte(netPack, 0)
    	DataPack.writeByte(netPack, 0)
	    
	    DataPack.flush(netPack)

        
        Actor.openDialogs(sysarg,enLinkMulphaAdventure)
	end
end


function GmMulphaAdventure(sysarg)
    local var = Actor.getStaticVar(sysarg)
    if not var.days then var.days = {} end


    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_MULPHAADVEN, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    local myDiceCount = myData[MYDICECOUNT] or 0

    myDiceCount = myDiceCount + 10000

    myData[MYDICECOUNT] = myDiceCount

    local newData = BaseFuc_serialize(myData)
    gRankMgr_SetRankEx(actorid, RANK_DEFINE_MULPHAADVEN, 1, newData)


    local netPack = DataPack.allocPacket(sysarg, LogicCmd.activitySys.sysId, LogicCmd.activitySys.sub.sMulphaAdventureGetDice)
    if not netPack then return end
    DataPack.writeByte(netPack, var.days.mulphaDailyDiceState or 0)
    --print("myDiceCount ="..myDiceCount)
    DataPack.writeShort(netPack, myDiceCount)
    DataPack.flush(netPack)
end

--活动开始
function GmMulphaAdventureStart()
    local var = System.getStaticVar()
    var.MulphaAdventureStart = 1
    if Lang.ScriptTips.MulphaAdventureTips014 and Lang.ScriptTips.MulphaAdventureTips014 ~= "" then
        System.broadcastTipmsg(Lang.ScriptTips.MulphaAdventureTips014,ttScreenCenter + ttChatWindow)
    end
end


function MulphaAdventureGetOnlineCount(sysarg)
    local todaylineTime = Actor.getTodayOnlineTime(sysarg)      --今日累积在线时间(秒)

    local allLimit = 0
    local count = #MulphaAdventureConfig.dailyOnlineDiceNumAdd
    for idx=1, count do
        local limitCfg = MulphaAdventureConfig.dailyOnlineDiceNumAdd[idx]
        if todaylineTime >= limitCfg.second then
            allLimit = allLimit + limitCfg.addCount
        else
            return allLimit
        end
    end
    return allLimit    
end
